﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using NetCore;
using NetCore.BLL;
using NetCore.Log;

namespace NetCore.Web.Controllers
{
   
    public class HomeController : BaseController
    {
        public IPageService mService { get; set; }
   
        // public  IUserService _service { get; set; }
        //public HomeController(IUserService service)
        //{
        //    _service = service;
        //}
        public IActionResult Index() {
            
          //  _service.Add();
            return View();
        }
        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }
        public IActionResult InitData()
        {
            try
            {
                string strError = "";
                NetCore.Code.BLL.InitDataService initDataService = new NetCore.Code.BLL.InitDataService();
                initDataService.InsertData(initDataService.DbRepository);
                if (initDataService.DeleteInitConfig())
                {
                    mService.InitPage(ref strError);
                }
               
                return Json(AjaxResult.Success("同步成功"));
            }
            catch (Exception ex)
            {
                return Json(AjaxResult.Error(ex.Message));
            }
          
        }
        public IActionResult Privacy()
        {
            return View();
        }

        public IActionResult Error()
        {
            var feature = HttpContext.Features.Get<IExceptionHandlerFeature>();
            var error = feature?.Error;
            ViewData["Message"] = error?.Message;
            ViewData["Exception"] = error;
            ViewData["StatusCode"] = HttpContext.Response.StatusCode;
            if (IsAjax)
            {
                return Json(AjaxResult.Error(error.Message, HttpContext.Response.StatusCode));
            }
            LogHelper.Error(feature.GetType(), error.Message);
            return View();
        }
       
        public async Task<IActionResult> LogOff()
        {
            await SignOutAsync();
            return RedirectToAction("Index", "Login");
        }
    }
}
